//
//  BIZHotTopicChildrenListVC.m
//  biz
//
//  Created by zhangfuwei on 2017/7/8.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

//vc
#import "BIZHotTopicChildrenListVC.h"
#import "BIZHotTopicDetailVC.h"
//cell
#import "BIZTopicImageCell.h"
#import "BIZTopicTextCell.h"
// model
#import "BIZHotTopicChildrenListModel.h"
#import "BIZHotTopicParentListModel.h"

@interface BIZHotTopicChildrenListVC ()<UITableViewDataSource,UITableViewDelegate>
/** tableView */
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation BIZHotTopicChildrenListVC{
    BIZHotTopicParentListChannelModel   *_channelM;
    BIZHotTopicChildrenListModel        *_childrenListM;
    NSString    *_uuid;
    int         _lastID;
}

-(instancetype)initWithChannelModel:(BIZHotTopicParentListChannelModel *)channelModel {
    self = [super init];
    if (self) {
        _channelM = channelModel;
        _uuid = channelModel.uuid;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"专题";
    [self initSubViews];
//    _lastID = 1;
//    [self requestHotTopicChildrenListWithPageIndex:_lastID pageEnd:_lastID+9];
    [_tableView.mj_header beginRefreshing];
}


- (void)initSubViews {
    [self.view addSubview:self.tableView];
}

#pragma mark -- requset
- (void)requestHotTopicChildrenListWithPageIndex:(int)idx pageEnd:(int)end {
    MJWeakSelf;
    [RequestTool POST:kHotTopicIssueeChildrenURL
           parameters:@{@"uuid":_uuid,@"pageIndex":@(idx),
                        @"pageEnd":@(end)}
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  [weakSelf stopRefresh];
                  DeLog(@"热点问题(zi级)--%@",responseObject);
                  BIZHotTopicChildrenListModel *model = [BIZHotTopicChildrenListModel yy_modelWithJSON:responseObject];
                  
                  if (model.code.intValue == 1) {
                      if (idx == 1) {
                          _childrenListM = model;
                      }else{
                          _childrenListM.data = [_childrenListM.data arrayByAddingObjectsFromArray:model.data];
                      }
                      _lastID += model.rows;
                      [_tableView reloadData];
                  }else if (model.code.intValue == 2){
                      [MBProgressHUD showTipMessageInView:@"请登录"];
                      [BIZUserModel shareUser].login = NO;
                      [weakSelf presentViewController:[BIZLoginViewController viewControllerFromStoryboard] animated:YES completion:nil];
                      
                  }else {
                      [MBProgressHUD showErrorMessage:model.des];
                      
                  }
              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  [MBProgressHUD showErrorMessage:@"网络不给力,请检测网络" timer:2.5];
                  [weakSelf stopRefresh];
                  DeLog(@"error ---%@",error);
                  
              }
     ];
}

#pragma mark -- tableView数据源&& 代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _childrenListM.data.count+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0 ) {
        BIZTopicImageCell *cell = [BIZTopicImageCell cellWithTableView:tableView indexPath:indexPath hidden:YES];
        cell.channelModel = _channelM;
        return cell;
    }
    
    BIZTopicTextCell *cell = [BIZTopicTextCell cellWithTableView:tableView indexPath:indexPath];
    cell.issueModel = [BIZHotTopicChildrenListIssueModel switchWithChildrenIssueModel:_childrenListM.data[indexPath.row-1]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 197;
    }
    return UITableViewAutomaticDimension;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) return;
    
    BIZHotTopicDetailVC *detailVC = [[BIZHotTopicDetailVC alloc] initWithUUID:_childrenListM.data[indexPath.row-1].uuid];
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

#pragma mark -- lazyLoad

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-64) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 50;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[BIZTopicImageCell class] forCellReuseIdentifier:kTopicImageCellID];
        [_tableView registerClass:[BIZTopicTextCell class] forCellReuseIdentifier:kTopicTextCellID];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _lastID = 1;
            [self requestHotTopicChildrenListWithPageIndex:_lastID pageEnd:_lastID+9];
        }];
        
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self requestHotTopicChildrenListWithPageIndex:_lastID pageEnd:_lastID+9];
        }];

    }
    return _tableView;
}

- (void)stopRefresh
{
    !self.tableView.mj_header.isRefreshing ? : self.tableView.mj_header.endRefreshing;
    !self.tableView.mj_footer.isRefreshing ? : self.tableView.mj_footer.endRefreshing;
}

@end
